package pe.com.trener.layer.model.persistence.daoimpl;

/**
 * @author:   Oscar Erick Candela Carbajal
 * @rol:	  Analista Programador Web Java
 * @proyecto: Portal Trener
 *
 */

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pe.com.trener.layer.model.persistence.dao.ClassBookDao;
import pe.com.trener.layer.model.pojo.ClassBook;

public class ClassBookDaoImpl extends HibernateDaoSupport implements ClassBookDao {

	public void crear(ClassBook objectModel) throws Exception {		
		try {
			getHibernateTemplate().save(objectModel);
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		} 			
	}
		
	public void modificar(ClassBook objectModel) throws Exception {		
		try {
			getHibernateTemplate().update(objectModel);
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		} 			
	}
	
	@SuppressWarnings("unchecked")
	public ClassBook buscarPorPk(Integer parametroPk) throws Exception {
		try {
	        List<ClassBook> resultQuery = new ArrayList<ClassBook>();
	        HibernateTemplate ht = getHibernateTemplate();
	        
	        ClassBook objectModel = new ClassBook();
	        		 	
		 	Object[] values = new Object[1];
	        values[0] = parametroPk;
  
	        String queryString = " from " +
								 " ClassBook o " +
								 " where " +
								 " 	  (o.codigoClassBook = ?) " +
								 " and o.estado = 1 ";
	        
	        resultQuery = ht.find(queryString, values);
	        
	        objectModel = (ClassBook)DataAccessUtils.uniqueResult(resultQuery);
	        
	        return objectModel;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	

	@SuppressWarnings("unchecked")
	public List<ClassBook> buscarPorTexto(String parametroTexto) throws Exception {
		try {
	        List<ClassBook> resultQuery = new ArrayList<ClassBook>();
		 	HibernateTemplate ht = getHibernateTemplate();
  
		 	Object[] values = new Object[3];
	        values[0] = parametroTexto;
	        values[1] = parametroTexto;
	        values[2] = parametroTexto;
	        
	        String queryString = " select o from " +
								 " ClassBook o, Semana s " +
								 " where " +
								 " ( " +
								 "    (o.anioAcademico.codigoAnio = ?) " +
								 " or (o.gradoSeccion.codigoGradoSeccion = ?) " +
								 " or (o.semana.descripcion like '%' + ? + '%') " +
								 " ) " +
								 " and o.semana.codigoSemana = s.codigoSemana " +
	        					 " and o.estado = 1 "+	        					 
	        					 " order by o.fechaInicio desc";	        
	        
	        resultQuery = (List<ClassBook>)ht.find(queryString, values);
	        
	        return resultQuery;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	
	
}